package com.hxc.pay.wx.service.impl;

import com.alibaba.fastjson.JSON;
import com.hxc.pay.wx.config.WxPayProperties;
import com.hxc.pay.wx.dto.NotifyDto;
import com.hxc.pay.wx.service.NotifyService;
import com.hxc.pay.wx.service.PaySuccessService;
import com.hxc.pay.wx.service.RefundSuccessService;
import com.wechat.pay.contrib.apache.httpclient.util.AesUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.security.GeneralSecurityException;
import java.util.Map;

@Slf4j
@Service
public class NotifyServiceImpl implements NotifyService {
    @Autowired
    private WxPayProperties wxPayProperties;
    @Autowired
    private ApplicationContext applicationContext;

    private PaySuccessService paySuccessService;

    private RefundSuccessService refundSuccessService;

    @Override
    public void notify(NotifyDto dto) {
        String jsonStr = null;
        try {
            jsonStr = new AesUtil(wxPayProperties.getApiV3Key().getBytes())
                    .decryptToString(dto.getResource().getAssociated_data().getBytes(),
                            dto.getResource().getNonce().getBytes(),
                            dto.getResource().getCiphertext());
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
            throw new RuntimeException("解析结果出错");
        }
        log.info("-----------微信主动通知支付结果------" + jsonStr);
        Map map = JSON.parseObject(jsonStr, Map.class);
        String outTradeNo = map.get("out_trade_no").toString();

        if (dto.getEvent_type().equals("TRANSACTION.SUCCESS") && "SUCCESS".equals(map.get("trade_state").toString())) {
            log.info("--------订单" + outTradeNo + "支付成功");
            // 自定义支付成功后续业务：需要实现com.hxc.pay.wx.service.PaySuccessService接口即可
            try {
                paySuccessService = applicationContext.getBean(PaySuccessService.class);
                paySuccessService.success(outTradeNo);
            } catch (BeansException e) {
                String message = e.getMessage();
                if (message.contains("com.hxc.pay.wx.service.PaySuccessService")) {
                    throw new RuntimeException("-----微信支付成功通知，请提供com.hxc.pay.wx.service.PaySuccessService接口实现类，自定义支付成功通知的业务");
                } else {
                    // 原样输出
                    e.printStackTrace();
                }
            }
        } else if (dto.getEvent_type().startsWith("REFUND") && "SUCCESS".equals(map.get("refund_status").toString())) {
            log.info("-----------订单" + outTradeNo + "退款成功");
            // 自定义退款成功后续业务
            try {
                refundSuccessService = applicationContext.getBean(RefundSuccessService.class);
                refundSuccessService.success(outTradeNo);
            } catch (BeansException e) {
                String message = e.getMessage();
                if (message.contains("com.hxc.pay.wx.service.RefundSuccessService")) {
                    throw new RuntimeException("-----微信退款成功通知，请提供com.hxc.pay.wx.service.RefundSuccessService，自定义退款成功通知的业务");
                } else {
                    // 原样输出
                    e.printStackTrace();
                }
            }
        } else {
            log.info("-----------未知通知操作------------");
        }
    }
}
